package com.java.connection;

import org.junit.Test;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.Properties;

/**
 * @author Jiange
 * @create 2021-03-30-13:01
 * Driver 就是接口
 */
public class ConnectonTest {
    @Test
    public void test1() throws SQLException {
        Driver driver = new com.mysql.cj.jdbc.Driver();
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        Connection connect = driver.connect("jdbc:mysql://localhost/test?serverTimezone=UTC",properties );
        PreparedStatement preparedStatement = connect.prepareStatement("select * from students where age in (?,?)");
        ResultSet keys = preparedStatement.getGeneratedKeys();
    }
    @Test
    //方式二
    public void test2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
        Class cls = Class.forName("com.mysql.jdbc.Driver");
        Constructor<com.mysql.jdbc.Driver> constructors = cls.getDeclaredConstructor();
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        Driver driver = constructors.newInstance();
        Connection connect = driver.connect("jdbc:mysql://localhost:3306/test", properties);
        System.out.println(connect);
    }
    @Test
    //方式三
    public void test3() throws Exception {
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Constructor<Driver> declaredConstructor = aClass.getDeclaredConstructor();
        Driver driver = declaredConstructor.newInstance();
        DriverManager.registerDriver(driver);//没有返回值  注册驱动
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        System.out.println("connection = " + connection);
    }
    @Test
    public void test4() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");//加载Driver类
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        System.out.println(connection);
    }
    //方式五  实现了解耦  如果需要修改配置文件信息 可以避免程序重新打包
    @Test
    public void test5() throws Exception {
        FileInputStream resourceAsStream = new FileInputStream("JDBC.properties");
        //InputStream resourceAsStream = ConnectonTest.class.getClassLoader().getResourceAsStream("JDBC.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");
        String url = properties.getProperty("url");
        System.out.println(user);
        System.out.println(driverClass);
        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }
}
